package com.zycfc.zsf.boot.util.ead;

import org.apache.commons.codec.binary.*;
import javax.crypto.spec.*;
import javax.crypto.*;
import java.security.*;
import java.io.*;
import org.slf4j.*;

public class DESUtils
{
    private static final Logger log;
    private static final String ALGORITHM = "DES";
    private static final String UTF_8 = "UTF-8";
    
    public static String encrypt(final String str, final String key) {
        String encryptionStr = "";
        if (!"".equals(str) && null != str) {
            try {
                encryptionStr = ByteUtils.toHexAscii(encryptData(str, key));
            }
            catch (Exception e) {
                DESUtils.log.error("encrypt error:", (Throwable)e);
                throw new RuntimeException("encrypt error", e);
            }
        }
        return encryptionStr;
    }
    
    public static String decrypt(final String enStr, final String key) {
        String decryptStr = "";
        try {
            decryptStr = decryptHex(key, enStr);
        }
        catch (Exception e) {
            DESUtils.log.error("decrypt error:", (Throwable)e);
            throw new RuntimeException("decrypt error");
        }
        return decryptStr;
    }
    
    private static String decryptHex(final String key, final String textHex) throws Exception {
        return new String(decrypt(Hex.decodeHex(textHex.toCharArray()), key), "UTF-8");
    }
    
    private static byte[] encryptData(final String text, final String key) {
        try {
            final SecretKeySpec securekey = new SecretKeySpec(key.getBytes("UTF-8"), "DES");
            final Cipher cipher = Cipher.getInstance("DES");
            cipher.init(1, securekey);
            return cipher.doFinal(text.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException e) {
            DESUtils.log.error("UnsupportedEncodingException", (Throwable)e);
        }
        catch (Exception e2) {
            DESUtils.log.error("Exception", (Throwable)e2);
        }
        return null;
    }
    
    private static byte[] decrypt(final byte[] data, final String key) {
        try {
            return decrypt(data, key.getBytes("UTF-8"));
        }
        catch (UnsupportedEncodingException e) {
            DESUtils.log.error("UnsupportedEncodingException", (Throwable)e);
        }
        catch (Exception e2) {
            DESUtils.log.error("Exception", (Throwable)e2);
        }
        return null;
    }
    
    private static byte[] decrypt(final byte[] data, final byte[] key) throws Exception {
        final SecretKeySpec securekey = new SecretKeySpec(key, "DES");
        final Cipher cipher = Cipher.getInstance("DES");
        cipher.init(2, securekey);
        return cipher.doFinal(data);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)DESUtils.class);
    }
}
